package com.hp.bon.sgw.wh.channel.groupConsistency.BizSlot;

import com.alibaba.fastjson.JSON;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.invoices.BizSlot;
import com.hp.bon.sgw.wh.channel.invoices.bizSlot.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: SGW_WH2.1_TMALL
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/20 11:52
 */
public class QryReturnBalanceDetailSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(QryReturnBalanceDetailSlot.class);
    @Override
    public BizResult doBiz(BizParam bizParam) {
        BizResult bizResult = new BizResult();
//        String method = bizParam.getParamString(BizConsts.KEY_REQUEST_METHOD);
//        String xAppId = bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_ID);
//        String xAppKey = bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY);
        String reqBody = bizParam.getParamString(BizConsts.KEY_REQUEST_BODY);
//        Map<String, Object> msgBody = new TreeMap<>();
//        if (!"POST".equals(method)) {
//            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
//            msgBody.put("resultCode", "01");
//            msgBody.put("resultMsg","该接口只支持POST提交");
//            String msgBodyStr = JSON.toJSONString(msgBody);
//            bizResult.setHttpBody(msgBodyStr);
//            return bizResult;
//        }
//        if (xAppId.isEmpty()||xAppKey.isEmpty()) {
//            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
//            msgBody.put("resultCode", "01");
//            msgBody.put("resultMsg","请求头信息错误");
//            String msgBodyStr = JSON.toJSONString(msgBody);
//            bizResult.setHttpBody(msgBodyStr);
//            return bizResult;
//        }
//        try {
//            if(StringUtils.isBlank(JSON.parseObject(reqBody).getJSONObject("svcObjectStruct").getString("objValue"))) {
//                throw new Exception();
//            }
//        }catch (Exception e) {
//            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
//            msgBody.put("resultCode", "1001");
//            msgBody.put("resultMsg","参数缺失");
//            String msgBodyStr = JSON.toJSONString(msgBody);
//            bizResult.setHttpBody(msgBodyStr);
//            return bizResult;
//        }

        // 获取URL地址
        String url = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

        String respStr = HttpClientUtils.postHttpStr(reqBody, url,bizParam);


        if ("".equals(respStr)) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            respStr=  HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL,"","","");
        }else {
            try {
            Map<String, Object> respStrMap = HttpClientUtils.parseJSON2Map(respStr);
            @SuppressWarnings("unchecked")
			List<Map<String, Object>> returnPlanInfo = (List<Map<String, Object>>) respStrMap.get("returnPlanInfo");
            for (Map<String, Object> stringObjectMap : returnPlanInfo) {
                stringObjectMap.remove("balanceTypeFlag");
            }
            respStr=JSON.toJSONString(respStrMap);
        } catch (Exception e) {
            _log.error("Failed to process external response message",e);
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            respStr = HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
            bizResult.setHttpBody(respStr);
            return bizResult;
        }

            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
        }
        _log.info("Responding to external messages====:{}",respStr);
        bizResult.setHttpBody(respStr);

        return bizResult;
    }
}
